﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Collections.Generic;
using System.Text;
public partial class Admin_adminProgramClass : basePage
{
    programClassBll pcBll = new programClassBll();
    private static readonly object objSync = new object();
    public int selectIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["action"]!=null)
            {

                string action = Request.QueryString["action"];
                switch (action.Trim())
                {
                    case "delAll":
                        string id = Request.QueryString["id"];
                        delAll(id);
                        break;
                    case "Insert":
                        add();
                        break;
                    case "editAll":                       
                        editAll();
                        break;
                    case "MoveAll":
                        MoveUpdate();
                        break;
                    case "edit":
                        edit();
                        break;
                    case "update":
                        Update();
                        break;
                    default:
                        break;
                }
            }
            ViewState["dt"] = pcBll.GetDataTable(0);
        }
    }

    #region 更新
    public void Update() 
    {
        programClass pc = new programClass();
        pc.programClassId = Convert.ToInt32(Request.QueryString["programid"]);
        pc.programClassName = Request.Form["pcname"].ToString();
        pc.programTag = Request.Form["pcTag"].ToString();
        pc.fID = Convert.ToInt32(Request.QueryString["id"]);
        if (!CommUtil.IsNumStr(Request.Form["pcsort"].ToString()) || Request.Form["pcsort"].ToString() == "")
        {
            Response.Write("1");
            Response.End();
        }
        if (pc.programClassName.Trim() == "")
        {
            Response.Write("2");
            Response.End();
        }
        pc.isSort = Convert.ToInt32(Request.Form["pcsort"]);
        pc.programTitle = Request.Form["programTitle"].ToString();
        pc.programKey = Request.Form["programKey"].ToString();
        pc.programDescription = Request.Form["programDescription"].ToString();
        if (pcBll.Update(pc))
        {

        }
    }
    #endregion

    #region  编辑
    public void edit() 
    {
        int pcid=Convert.ToInt32(Request.QueryString["id"]);
        programClass pc = pcBll.GetModel(pcid);
        this.pcid_hid.Value = pcid.ToString();
        this.m_pcname.Value = pc.programClassName;
        this.m_pcsort.Value = pc.isSort.ToString();
        this.m_pcTag.Value = pc.programTag;
        selectIndex = pc.fID;
        this.programTitle.Value = pc.programTitle;
        this.programKey.Value = pc.programKey;
        this.programDescription.Value = pc.programDescription;
    }
    #endregion

    #region 删除
    public void delAll(string id)
    {
        if (id.Contains(","))
        {
            string[] idParam = id.Split(',');
            string[] pClassid = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                if (pcBll.ExistsChildren(idParam[i]))
                {
                    Response.Write("<font color='red'>有子类的不能删除，请先删除子类！</font>");
                    return;
                }
                pClassid[i] = idParam[i];
            }
            if (pcBll.delete(pClassid))
            {

            }
        }
        else 
        {
            if (pcBll.ExistsChildren(id))
            {
                Response.Write("<font color='red'>有子类的不能删除，请先删除子类！</font>");
                return;
            }
            else 
            {
                if (pcBll.Delete(Convert.ToInt32(id)))
                {

                }
            }
        }
    }
    #endregion

    #region 移动
    public void MoveUpdate() 
    {
        string id=Request.QueryString["id"];
        string pcid = Request.QueryString["pcid"];
        if (pcid.Contains(","))
        {
            string[] idParam = pcid.Split(',');
            string[] pcs = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                pcs[i] = idParam[i];
            }
            if (pcBll.Update(pcs,id))
            {

            }
        }
        else 
        {
            string[] pcids ={pcid};
            if (pcBll.Update(pcids,id))
            {

            }
        }
    }
    #endregion

    #region 添加
    public void add() 
    {
        programClass pc = new programClass();
        pc.programClassName = Request.Form["pcname"].ToString();
        pc.programTag = Request.Form["pcTag"].ToString();        
        pc.fID = Convert.ToInt32(Request.QueryString["id"]);        
        if (!CommUtil.IsNumStr(Request.Form["pcsort"].ToString()) || Request.Form["pcsort"].ToString()=="")
        {
            Response.Write("1");
            Response.End();
        }
        if (pc.programClassName.Trim()=="")
        {
            Response.Write("2");
            Response.End();
        }
        pc.isSort = Convert.ToInt32(Request.Form["pcsort"]);
        pc.programTitle = Request.Form["programTitle"].ToString();
        pc.programKey = Request.Form["programKey"].ToString();
        pc.programDescription = Request.Form["programDescription"].ToString();
        if (pcBll.Insert(pc))
        {

        }
    }

    #endregion

    #region 绑定
    public string bindProgramClassList(int ParentID, int length, StringBuilder sb) 
    {
        DataView dvTree =pcBll.GetDataView(ViewState["dt"] as DataTable,ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            sb.Append("<tr align=\"center\">");
            if (length == 1)
            {
                sb.AppendFormat("<td align=\"left\" style=\"width: 21%; height: 18px\">{0}<input type='checkbox' name='m_id' value='{1}'  class='checkbox'  id='{1}' />{1}.<a href='../zh/Completeprogram-{1}.html'>{2}</a>(<font color='red'>{3}</font>)</td>", str, row["programClassId"].ToString(), row["programClassName"], countByProgramClass(row["programClassId"]));
            }
            else
            {
                sb.AppendFormat("<td align=\"left\" style=\"width: 21%; height: 18px\">{0}<input type='checkbox' name='m_id' value='{1}'  class='checkbox'  id='{1}' />{1}.<a href='javascript:void(0)'>{2}</a>(<font color='red'>{3}</font>)</td>", str, row["programClassId"].ToString(), row["programClassName"], countByProgramClass(row["programClassId"]));
            }
            sb.AppendFormat("<td style=\"width: 21%; height: 18px\"><input style=\"width: 80%;\" size='13' type='text' id='pcname{0}' value='{1}' ></td>", row["programClassId"].ToString(), row["programClassName"].ToString());
            sb.AppendFormat(" <td style=\"width: 18%; height: 18px\"><input style=\"width: 80%;\" size='10' type='text' id='pcTag{0}' value='{1}'></td>", row["programClassId"].ToString(), row["programTag"].ToString());
            sb.AppendFormat("<td style=\"width: 18%; height: 18px\"><input size='12' type='text' id='isSort{0}' value='{1}'></td>", row["programClassId"], row["isSort"]);
            sb.Append("<td style=\"height: 18px\"><input type='button' value='编辑' onClick=\"edit(" + row["programClassId"].ToString() + ")\" name='Input' class='btn' title='" + row["programClassId"].ToString() + "'/> <input type='button' value='删除' onClick=\"if(confirm('确定要删除吗')){$('#loading').show();ajax_get('?action=delAll&id='+this.title,'',function(msg){$('#cpcontainer').empty().append(msg);$('#loading').hide();});}else{return false}\" name='Input' class='btn' title='" + row["programClassId"].ToString() + "'/><input  type='hidden' id='pFid" + row["programClassId"].ToString() + "'  value='" + row["fID"].ToString() + "'/><input id=\"programTitle" + row["programClassId"].ToString() + "\" value=\"" + row["programTitle"].ToString() + "\"  type=\"hidden\" /><input id=\"programKey" + row["programClassId"].ToString() + "\" value=\"" + row["programKey"].ToString() + "\" type=\"hidden\" /><input id=\"programDescription" + row["programClassId"].ToString() + "\" value=\"" + row["programDescription"].ToString() + "\" type=\"hidden\" /></td>");
            sb.Append("</tr>");
            bindProgramClassList(Convert.ToInt32(row["programClassId"]),length,sb);
        }
        return sb.ToString();
    }
    #endregion

    #region ajax批量修改
    public void editAll() 
    {
        string id = Request.QueryString["id"];
        if (id.Contains(","))
        {
            lock (objSync)
            {
                string[] idParam = id.Split(',');
                for (int i = 0; i < idParam.Length; i++)
                {
                    programClass pc = new programClass();
                    pc.programClassName = Request.Form["pcname" + idParam[i] + ""].ToString();
                    pc.programClassId = Convert.ToInt32(idParam[i]);
                    pc.programTag = Request.Form["pcTag" + idParam[i] + ""].ToString();
                    if (!CommUtil.IsNumStr(Request.Form["pcsort" + idParam[i] + ""].ToString()) || Request.Form["pcsort" + idParam[i] + ""].ToString() == "")
                    {
                        Response.Write("1");
                        Response.End();
                    }
                    if (pc.programClassName.Trim() == "")
                    {
                        Response.Write("2");
                        Response.End();
                    }
                    pc.isSort =Convert.ToInt32(Request.Form["pcsort" + idParam[i] + ""]);
                    pc.fID = Convert.ToInt32(Request.Form["pFid" + idParam[i] + ""]);
                    pc.programTitle = Request.Form["programTitle" + idParam[i] + ""].ToString();
                    pc.programKey = Request.Form["programKey" + idParam[i] + ""].ToString();
                    pc.programDescription = Request.Form["programDescription" + idParam[i] + ""].ToString();
                    if (pcBll.Update(pc))
                    {
                    }
                    else
                        return;
                }
            }
        }
        else 
        {
            programClass pc = new programClass();
            pc.programClassName = Request.Form["pcname" + id + ""].ToString();
            pc.programClassId = Convert.ToInt32(id);
            pc.programTag = Request.Form["pcTag" + id + ""].ToString();
            if (!CommUtil.IsNumStr(Request.Form["pcsort" + id + ""].ToString()) || Request.Form["pcsort" + id + ""].ToString() == "")
            {
                Response.Write("1");
                Response.End();
            }
            if (pc.programClassName.Trim() == "")
            {
                Response.Write("2");
                Response.End();
            }
            pc.isSort =Convert.ToInt32( Request.Form["pcsort" + id + ""]);
            pc.fID = Convert.ToInt32(Request.Form["pFid" + id + ""]);
            pc.programTitle = Request.Form["programTitle" + id + ""].ToString();
            pc.programKey = Request.Form["programKey" + id+ ""].ToString();
            pc.programDescription = Request.Form["programDescription" + id + ""].ToString();
            if (pcBll.Update(pc))
            {
            }
            else
                return;
        }
    }
    #endregion

    #region 根据类别id获得下面的项目数量
    public string countByProgramClass(object id)
    {
        if (id != null)
        {
            int i = Convert.ToInt32(id);
            ProgramBll pBll = new ProgramBll();
            return pBll.countByProgramClass(i).ToString();
        }
        else
        {
            return null;
        }
    }
    #endregion

    #region 自定义下拉框
    public string programClassDp()
    {
        StringBuilder str = new StringBuilder();        
        str.Append("<option value='0'>---顶级分类---</option>");
        str.Append(AddItems(0, 0, new StringBuilder()));        
        return str.ToString();

    }

    public string AddItems(int ParentID, int length, StringBuilder sb) 
    {
        DataView dvTree =pcBll.GetDataView(ViewState["dt"] as DataTable,ParentID);
        length++; 
        foreach (DataRowView row in dvTree)
        {   
            string str = addtab(length);
            if (Convert.ToInt32(row["programClassId"]) == selectIndex)
            {
                sb.AppendFormat("<option selected='selected' value='" + row["programClassId"] + "'>{0}</option>", str + "|--" + row["programClassName"]);
            }
            else
            {

                sb.AppendFormat("<option value='" + row["programClassId"] + "'>{0}</option>", str + "|--" + row["programClassName"]);
            }       
            
            AddItems(Convert.ToInt32(row["programClassId"]), length, sb);
        }
        return sb.ToString();

    }

    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {

                mark += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        return mark;
    }
    #endregion
}
